package com.huika.dealontop.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.huika.dealontop.db.DealOnTopDBHelper;
import com.huika.dealontop.db.bean.SearchHotKey;
import com.huika.dealontop.db.dao.impl.BaseDao;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huhao on 2016/1/26.
 */
public class SearchHotDao implements BaseDao<SearchHotKey> {

    private Context mContext;
    private DealOnTopDBHelper mDBHelper;

    public SearchHotDao(Context context){
        this.mContext = context;
        //拿到DBHelper
        mDBHelper = new DealOnTopDBHelper(this.mContext);
    }

    @Override
    public void add(SearchHotKey o) {
        //获取一个可写类型的数据库操作类
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DealOnTopDBHelper.SearchHot.KEY, o.getKey());
        values.put(DealOnTopDBHelper.SearchHot.COUNT, o.getCount());
        values.put(DealOnTopDBHelper.SearchHot.TIME, System.currentTimeMillis());
        db.insert(DealOnTopDBHelper.SEARCH_HOT, null, values);
        db.close();
    }

    @Override
    public void delete(int _id) {

    }

    @Override
    public void update(SearchHotKey o) {
        //获取一个可写类型的数据库操作类
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("s_key", o.getKey());
        values.put("s_count", o.getCount());
        values.put("s_time", System.currentTimeMillis());
        String select[] = {String.valueOf(o.get_id())};
        db.update("search_hot", values, "_id=?", select);
        db.close();
    }

    @Override
     public SearchHotKey query(int _id) {
        //获取一个可读类型的数据库操作类
        SQLiteDatabase db = mDBHelper.getReadableDatabase();
        String columns[] = {"_id","s_key","s_count"};
        String select[] = {String.valueOf(_id)};
        Cursor cursor = db.query("search_hot", columns, "_id=?", select, null, null, null);
        SearchHotKey result = null;
        if(cursor != null && cursor.moveToFirst()){
            result = new SearchHotKey();
            result.set_id(cursor.getInt(0));
            result.setKey(cursor.getString(1));
            result.setCount(cursor.getInt(2));
        }
        cursor.close();
        db.close();
        return result;
    }

    public SearchHotKey query(String key) {
        //获取一个可读类型的数据库操作类

        SQLiteDatabase db = mDBHelper.getReadableDatabase();
        String[] args = {String.valueOf(key)};
        Cursor cursor = db.query(DealOnTopDBHelper.SearchHot.NAME, null, "s_key=?", args, null, null, null, null);
        SearchHotKey result = null;
        if(cursor != null && cursor.moveToFirst()){
            result = new SearchHotKey();
            result.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
            result.setKey(cursor.getString(cursor.getColumnIndex(DealOnTopDBHelper.SearchHot.KEY)));
            result.setCount(cursor.getInt(cursor.getColumnIndex(DealOnTopDBHelper.SearchHot.COUNT)));
        }
        cursor.close();
        db.close();
        return result;
    }

    public List<SearchHotKey> getTOP6(){
        //获取一个可读类型的数据库操作类
        SQLiteDatabase db = mDBHelper.getReadableDatabase();
        String columns[] = {"_id","s_key","s_count"};
        Cursor cursor = db.query("search_hot", columns, null, null, null, null, "s_count desc,s_time desc");
        List<SearchHotKey> list = new ArrayList<>();
        SearchHotKey result;
        while (cursor.moveToNext() && cursor.getPosition() < 6){
            result = new SearchHotKey();
            result.set_id(cursor.getInt(0));
            result.setKey(cursor.getString(1));
            result.setCount(cursor.getInt(2));
            list.add(result);
        }
        cursor.close();
        db.close();
        return list;
    }

}


